﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessAnswer
    {
        #region Create
        public static void createAnswer(Answer a)
        {
            try
            {
                dataAnswer.createAnswer(a); 
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating answer");
            }
        }

        public static void createAnswer(Question q, Expert e, String text)
        {
            try
            {
                Answer a = new Answer();
                a.QuestionID = q.QuestionID;
                a.ExpertID = e.ExpertID;
                a.Text = text;
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error creating answer");
            }
        }
        #endregion

        #region Get
        public static Answer getAnswer(Guid guid)
        {
            try
            {
                return dataAnswer.getAnswer(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting answer");
            }
        }

        public List<Answer> getAnswersByQuestion(Question q)
        {
            try
            {
               Expression<Func<Answer, bool>> whereClause = (Answer a) => (a.QuestionID == q.QuestionID);

               return dataAnswer.getAnswers(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting answers by question");
            }
        }

        public List<Answer> getAnswersByQuestionAndString(Question q, String match)
        {
            try
            {
                char[] delimiters = new char[]{ '\n', ',', '.', '\t', ' ' };
                string[] words = match.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                List<Answer> answers = new List<Answer>();
                Expression<Func<Answer, bool>> whereClause;
                
                foreach(String word in words){
                    whereClause = (Answer a) => (a.QuestionID == q.QuestionID) && (a.Text.Contains(word)) && (answers.Contains(a) == false);
                    List<Answer> result = dataAnswer.getAnswers(whereClause).ToList();
                    answers.AddRange(result);
                }

                return answers;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting answer by question and string match");
            }
        }

        public List<Answer> getAnswersByQuestionAndExpert(Question q, Expert e)
        {
            try
            {
                Expression<Func<Answer, bool>> whereClause = (Answer a) => (a.ExpertID == e.ExpertID) && (a.QuestionID == q.QuestionID);

                return dataAnswer.getAnswers(whereClause).ToList();
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error getting answer by question and expert");
            }
        }

        public List<Answer> getAnswersByExpert(Expert e)
        {
            try
            {
                Expression<Func<Answer, bool>> whereClause = (Answer a) => (a.ExpertID == e.ExpertID);

                return dataAnswer.getAnswers(whereClause).ToList();
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error getting answer by expert");
            }
        }
        #endregion

        #region Set
        public static Boolean changeAnswer(Answer a)
        {
            try
            {
                a.Date = DateTime.Now;
                return dataAnswer.changeAnswer(a);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing answer");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteAnswer(Answer a)
        {
            try
            {
                return dataAnswer.deleteAnswer(a);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deletting answer");
            }
        }
        #endregion
    }
}
